package com.workplan.client.widgets.tabs;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.general.client.action.ActionHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.workplan.client.modules.Modules;
import com.workplan.client.modules.events.SwitchModuleEvent;

public abstract class AbstractTabPresenter extends
		WidgetPresenter<TabPresenter.Display> implements TabPresenter {

	public AbstractTabPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
	}

	protected abstract Modules getThisModule();

	@Override
	protected void onBind() {
		registerHandler(display.addActionHandler(new ActionHandler() {

			@Override
			public <H extends EventHandler> void onAction(GwtEvent<H> event) {
				// Window.alert(getThisModule().name());
				eventBus.fireEvent(new SwitchModuleEvent(getThisModule()));
			}
		}));

		registerHandler(eventBus.addHandler(SwitchModuleEvent.TYPE,
				new SwitchModuleEvent.Handler() {

					@Override
					public void onSwitchModule(SwitchModuleEvent event) {
						if (event.getNewModule().equals(getThisModule())) {
							display.setSelected(true);
						} else {
							display.setSelected(false);
						}
					}
				}));

	}

	@Override
	protected void onRevealDisplay() {
		// do nothing

	}

	@Override
	public HandlerRegistration addActionHandler(ActionHandler handler) {
		return display.addActionHandler(handler);
	}

	@Override
	public void setSelected(boolean selected) {
		display.setSelected(selected);
	}
}
